$(function(){
	movie_index.run();
});

var movie_index = {
	run: function(){
		this._init_();
		this._start_();
		this._bind_events();
	},
	
	_init_: function(){		
		this.hasMore = $("#show_content_div > :hidden.hasMore").val() || false;
		this.category = $("#category").val();
		this.genre = '';
		this.initial = '';
		this.pageSize = 30;
		this.scrollPage = 2;
		this.loadingDiv = $("#loading_div").html();
	},
	
	_start_: function(){		
		this._highlight_nav();
		
		var loc = location.href;
		var index = loc.indexOf("#");
		if(index > 0){
			var params = loc.substring(index + 1);
			
			this._highlight_genre_or_alpha(params);
			
			$("#show_content_div").html(this.loadingDiv);
			
			this._query_movie(1,params);
		}
		
		this._adjust_layout();
	},
	
	
	_highlight_nav: function(){
		var index = 0;
		if(this.category == 'trailer'){
			index = 1;
		}
		$("#side_menu > ul > li").eq(index).addClass("curr");
	},
	
	_bind_events: function(){
		//genre
		$("#genres_ul > li > a").click(function(){
			var $this = $(this);
			var g = $this.attr("genre");
			if(g != movie_index.genre){
				$("#genres_ul > li > a").removeClass("selected");
				$this.addClass("selected");
				$("#alpha_div > a").removeClass("selected").eq(0).addClass("selected");		
				movie_index._handle_genre_clicked(g);	
			}						
			
			return false;
		});
		
		$("#alpha_div > a").click(function(){
			var $this = $(this);
			var alpha = $this.attr("alpha");
			if(alpha == "all"){
				alpha="";
			}
			$("#alpha_div > a").removeClass("selected");
			$this.addClass("selected");
			movie_index._handle_alpha_clicked(alpha);
			return false;
		});
		
		this._bind_image_events(("#show_content_div"));	
		
		//scrolling
		$.scroll(function(){
			movie_index._handle_scrolling();
		});
		
		$("#gotoTop").easeup();
		
		$(window).wresize(function() {
			movie_index._adjust_layout();
		});
		
		$("#open_side_btn").click(function(){
			movie_index._adjust_layout("open");
		});
		
		$("#close_side_btn").click(function(){
			movie_index._adjust_layout();
		});
	},
	
	_bind_image_events: function($div){
		$("div.mi_box > div.img", $div).mouseenter(function(){
			$(this).children('div.tc_btn').fadeIn("slow");
		}).mouseleave(function(){
			$(this).children('div.tc_btn').stop(true,false).fadeOut("fast");
		});
		
		$("div.mi_box a", $div).click(function(){				
			var $this = $(this);
			var href = $this.attr("href");			
			if('javascript:;' == href){
				return;
			}			
			if(movie_index.genre){
				href += "#genre=" + movie_index.genre;
			}			
			location.href = href;
			return false;
		});
	},
	
	_handle_genre_clicked: function(genre){		
		this.genre = genre;
		this.initial = "";
		this._change_the_location();		
		this.scrollPage = 1;	
		//reset content holder
		$("#show_content_div").html(this.loadingDiv);		
		this._query_movie();	
	},
	
	_handle_alpha_clicked: function(alpha){		
		this.initial = alpha;
		this._change_the_location();
		//reset scrollPage
		this.scrollPage = 1;	
		//reset content holder
		$("#show_content_div").html(this.loadingDiv);			 
		this._query_movie();	
	},
	
	_highlight_genre_or_alpha: function(params){
		var args = params.getArgs();
		var genre = args['genre'];
		var initial = args['initial'];
		this.genre = genre;
		this.initial = initial;
		
		if(genre){
			$("#genres_ul > li > a").removeClass("selected");
			$("#genres_ul > li > a").each(function() {
				var $this = $(this);
				if($this.attr("genre") == genre){
					$this.addClass("selected");
					return false;
				}
			});
		}
		
		if(initial){
			$("#alpha_div > a").removeClass("selected");
			$("#alpha_div > a").each(function() {
				var $this = $(this);
				if($this.attr("alpha") == initial){
					$this.addClass("selected");
					return false;
				}
			});
		}
	},
	
	_change_the_location: function(){
		var loc = location.href;
		var params = this._gather_params();
		if(params.length > 0){
			loc = loc.substring(0,loc.indexOf("#"));
			location.href = loc + "#" + params; 
		}
		return false;
	},
	
	_handle_scrolling: function(){	
		if(this.hasMore){
			var $w = $(window);
			if( ! $w.data("scrolling")){
				$w.data("scrolling", true);
				movie_index._query_movie(this.scrollPage);
			}
		}
	},
	
	_query_movie: function(page,params){
		var $w = $(window);
		
		var $loading = $("#show_content_div > div.loadingBox");
		$loading.show();
		page = page || 1;
		var data = null;
		if(params){
			data = params;
		}else{
			data = this._gather_params();
		}		
		
		var url = main.contextPath + "/world/movie/" + this.category + "/query?page=" + page;
		$.get(url, data, function(result){
			movie_index._check_has_more(result);
			
			result = movie_index._bind_fly_image_events(result);
			$loading.before(result);
			
			movie_index._adjust_layout();
		}).complete(function(){			
			$w.data("scrolling", false);
			$loading.hide();			
		});
	},
	
	_bind_fly_image_events: function(result){
		var $div = $("<div>" + result + "</div>");
		this._bind_image_events($div);
		return $div.children();
	},
	
	_gather_params: function(){
		var ps = [];
		if(this.genre){
			ps.push("genre=" + this.genre);
		}
		if(this.initial){
			ps.push("initial=" + this.initial);
		}
		if(ps){
			return ps.join("&");
		}
	},
	
	_check_has_more: function(result){
		var $result = $("<div>" + result + "</div>");
		var m = $(":hidden.hasMore", $result).val();
		if(m){
			this.hasMore = true;
			this.scrollPage += 1;
		}else{
			this.hasMore = false;
		}
	},
	
	_adjust_layout: function(action){
		var boxWidth = $("#show_content_div").width()-5;
		if(action == 'open'){
			boxWidth = boxWidth - 180;
		}
		var account = Math.floor(boxWidth/160);
		var paddingValue = 20 + (boxWidth - 160*account + 20)/(account-1);
		var flag = 1;
		$("#show_content_div > div.m_box5").each(function(){
			$(this).css("padding-right",0);
			if(flag < account){
				if(flag == 1){
					$(this).css("padding-left",0);
					flag++;
				}else{
					$(this).css("padding-left",paddingValue);
					flag++;
				}
			}else{
				$(this).css("padding-left",paddingValue);
				flag = 1;
			}
		});
	}
};